package com.example.my_music_palyer.common.tools;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * 密码生成工具类
 * 用于生成管理员账户的密码哈希
 */
public class PasswordGenerator {
    
    /**
     * 生成密码哈希
     * @param password 原始密码
     * @return 哈希后的密码
     */
    public static String generateHash(String password) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        return encoder.encode(password);
    }
    
    /**
     * 验证密码
     * @param rawPassword 原始密码
     * @param encodedPassword 哈希后的密码
     * @return 是否匹配
     */
    public static boolean verifyPassword(String rawPassword, String encodedPassword) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        return encoder.matches(rawPassword, encodedPassword);
    }
    
    public static void main(String[] args) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        
        // 生成admin用户的密码哈希
        String adminPassword = "123456";
        String hashedPassword = encoder.encode(adminPassword);
        
        System.out.println("=== 密码哈希生成 ===");
        System.out.println("原始密码: " + adminPassword);
        System.out.println("哈希值: " + hashedPassword);
        System.out.println();
        
        // 验证密码是否正确
        boolean isValid = encoder.matches(adminPassword, hashedPassword);
        System.out.println("密码验证结果: " + isValid);
        System.out.println();
        
        // 生成SQL插入语句
        System.out.println("=== SQL语句 ===");
        System.out.println("INSERT INTO `user` (`username`, `password`, `role`) VALUES ('admin', '" + hashedPassword + "', 'admin');");
        
        // 测试现有的哈希值
        System.out.println();
        System.out.println("=== 测试现有哈希值 ===");
        String existingHash = "$2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iAt6Z5EHsM8lE9lBOsl7iKTVEFDa";
        boolean existingValid = encoder.matches(adminPassword, existingHash);
        System.out.println("现有哈希值验证结果: " + existingValid);
    }
}
